package com.cindata

import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*
import grails.transaction.Transactional

@Transactional(readOnly = true)
@Secured(['isAuthenticated()'])
class AccountFlowController {

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max) {
        params.max = Math.min(max ?: 10, 100)
        respond AccountFlow.list(params), model:[accountFlowCount: AccountFlow.count()]
    }

    def show(AccountFlow accountFlow) {
        respond accountFlow
    }

    def create() {
        def targetUri = request.getHeader("referer")
        params.targetUri = targetUri
        respond new AccountFlow(params)
    }

    @Transactional
    def save(AccountFlow accountFlow) {
        def account = Account.findById(params["account"])
        if (accountFlow == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountFlow.hasErrors()) {
            transactionStatus.setRollbackOnly()
            println accountFlow.errors
            respond accountFlow.errors, view:'create'
            return
        }
        if(account == null){
            transactionStatus.setRollbackOnly()
            flash.message = "银行机构不能为nul"
            respond accountFlow, view:'create'
            return
        }
        accountFlow.save flush:true
        def targetUri = params["targetUri"]

        redirect url: targetUri, params: [id: accountFlow?.account?.id]
    }

    def edit(AccountFlow accountFlow) {
        respond accountFlow
    }

    @Transactional
    def update(AccountFlow accountFlow) {
        if (accountFlow == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (accountFlow.hasErrors()) {
            transactionStatus.setRollbackOnly()
            respond accountFlow.errors, view:'edit'
            return
        }

        accountFlow.save flush:true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'accountFlow.label', default: 'AccountFlow'), accountFlow.id])
                redirect accountFlow
            }
            '*'{ respond accountFlow, [status: OK] }
        }
    }

    @Transactional
    def delete(AccountFlow accountFlow) {

        if (accountFlow == null) {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }
        def targetUri = request.getHeader("referer")
        accountFlow.delete flush:true

        redirect url: targetUri, params: [id: accountFlow?.account?.id]
    }

    protected void notFound() {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'accountFlow.label', default: 'AccountFlow'), params.id])
                redirect action: "index", method: "GET"
            }
            '*'{ render status: NOT_FOUND }
        }
    }
}
